//creare capitol pe client
//varianta compatibila Mozilla

function xmlChapter(chapName, chapId, url){
	/**
	* constructor
	* @param String chapName numele noului capitol
	* @param String chapId atributul id al noului cap
	* @return void
	* @requires xmlBasicElements
	*/
	//alert(chapName+"+"+chapId+"+"+url);
	var docType=document.implementation.createDocumentType("chapter", null, "../../include/dtd/xmlChapter.dtd");
	this.chapHandler=document.implementation.createDocument(null, "chapter", docType);

	this.chapHandler.async=false;

	this.chapName=chapName;
	//this.chapId=chapId;

	this.newElemQueue=new Array();
	this.newElemQueueCounter=0;
	this.changesQueue=new Array();
	this.changesQueueCounter=0;

	if(url == null){
		var basicStruct="<chapter chapId='" +chapId+"'><title>"+chapName+"</title></chapter>";
		try{
			var newParser=new DOMParser();
			var newXmlDom=newParser.parseFromString(basicStruct, "text/xml");

			this.chapHandler.removeChild(this.chapHandler.documentElement);

			for(var i=0; i< newXmlDom.childNodes.length; i++){
				var newNode=this.chapHandler.importNode(newXmlDom.childNodes[i], true);
				this.chapHandler.appendChild(newNode);
			}

			this.chapRoot=this.chapHandler.documentElement;
			//return this.chapHandler;
		}catch(e){
			alert("catch message: "+e.message);
		}
	}else{
		//load chapter from source
		this.chapId=chapId;
		this.chapHandler.load(url);
		this.chapRoot=this.chapHandler.documentElement;
		//alert(chapRoot.xml);
		this.chapURL=url;
		//return this.chapHandler;
	}
	//checkBrowser();

	/**
	* afisare continut document in alert
	*/
	this.printDocument=function(){
		alert(this.chapHandler.xml);
	}

	/**
	* creare paragraf nou
	*/
	//updated
	this.createNewParagraph=function(id, text){
		newParag=new xmlBasicElements(this.chapHandler);
		this.newElemQueue[this.newElemQueueCounter]=newParag.createParagraph(id, text);
		//alert(this.newElemQueue[this.newElemQueueCounter]);
		this.newElemQueueCounter+=1;
	}

	/**
	* creare nota noua
	*/
	this.createNewNote=function(id, text){
		newNote=new xmlBasicElements(this.chapHandler);
		this.newElemQueue[this.newElemQueueCounter]=newNote.createNote(id, text);
		//alert(this.newElemQueue[this.newElemQueueCounter]);
		this.newElemQueueCounter+=1;
	}

	/**
	*	creare tabel nou
	*/
	this.createNewTable=function(id, title, headers){
		newTable=new xmlBasicElements(this.chapHandler);
		this.newElemQueue[this.newElemQueueCounter]=newTable.createTable(id, title, headers);
		//alert(this.newElemQueue[this.newElemQueueCounter]);
		this.newElemQueueCounter+=1;
	}

	/**
	*	update elemente simple (paragraf, nota, citat)
	*/
	this.updateBasicElements=function(id, text, type){
		//alert(id+"_"+text+"_"+type);
		switch(type){
			case 'paragraph':
			newParag=new xmlBasicElements(this.chapHandler);
			this.changesQueue[this.changesQueueCounter]=newParag.updateParagraph(id, text);
			this.changesQueueCounter+=1;
			//alert("dsfdsf");
			break;

			case 'note':
			alert("note");
			break;

			case 'quote':
			alert("quote");
			break;
		}
	}



	/**
	*	afisare continut
	*/
	this.printToHtmlForAuthor=function(bigDocName){
		var XslDom=new XmlDom();
		XslDom.async=false;
		XslDom.load('include/xsl/xmlChapterForAuthors.xsl');

		var newProcessor=new XSLTProcessor();
		newProcessor.importStylesheet(XslDom);
		var cale="../../documente/"+bigDocName+"/"+this.chapName+"Comments.xml";
		newProcessor.setParameter(null, "caleComment", cale);

		var newResultDom=newProcessor.transformToDocument(this.chapHandler);
		var newResult=newResultDom.xml;
		newResult=newResult.substring(newResult.indexOf(">")+1, newResult.lastIndexOf("<"));


		document.getElementById('helder').innerHTML=newResult;
	}

	this.printToHtmlForFriend=function(bigDocName, currentUser){
		var XslDom=new XmlDom();
		XslDom.async=false;
		XslDom.load('include/xsl/xmlChapterForPartners.xsl');

		var newProcessor=new XSLTProcessor();
		newProcessor.importStylesheet(XslDom);
		var cale="../../documente/"+bigDocName+"/"+this.chapName+"Comments.xml";
		newProcessor.setParameter(null, "caleComment", cale);
		newProcessor.setParameter(null, "currentUser", currentUser);

		var newResultDom=newProcessor.transformToDocument(this.chapHandler);
		var newResult=newResultDom.xml;
		newResult=newResult.substring(newResult.indexOf(">")+1, newResult.lastIndexOf("<"));


		document.getElementById('helder').innerHTML=newResult;
	}


	return this;
}